from django.db.models.signals import post_save
from notifications.signals import notify

from two_comment.models import CommentReply, Comment
from users.models import User


def comment_handler(sender, instance, created, **kwargs):
    # 避免接收自己的通知
    recipients = User.objects.exclude(id=instance.author.id)
    if instance.entry.author in recipients:
        notify.send(instance.author,
                    recipient=instance.entry.author,
                    verb='评论了你',
                    action_objection=instance,
                    target=instance.entry,
                    description=instance.content
                    )


post_save.connect(comment_handler, sender=Comment)


def reply_handler(sender, instance, created, **kwargs):
    # 避免接收自己的通知
    recipients = User.objects.exclude(id=instance.reply_from.id)
    if instance.reply_to in recipients:
        notify.send(instance.reply_from,
                    recipient=instance.reply_to,
                    verb='回复了你',
                    action_objection=instance,
                    target=instance.comment,
                    description=instance.content
                    )
    else:
        pass


post_save.connect(reply_handler, sender=CommentReply)
